package com.dong.cutesms.models;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DBHelper_DB extends SQLiteOpenHelper {

	public  static String DATABASENAME = "cutesms.db";
	public  String PEOPLE_TABLE = "peoples";
	public  String colPeopleId = "peopleid";
	public  String colPhoneNumber = "phonenumber";
	public  String colDisplayName = "peoplename";
	public  String colHasSms = "hassms";
	public  String colDate = "date";
	public  String colThreadID = "threadid";
	public  String colRecentMes = "recentmes";
	public  String colAvatarID = "avatarid";
	
	public  String CONFIG_TABLE = "config";
	public  String colID = "id";
	public  String colKey = "conf_key";
	public  String colValue = "conf_value";

	private ArrayList<PeopleModel> peopleList = new ArrayList<PeopleModel>();
	
	Context c;

	public DBHelper_DB(Context context) {
		super(context, DATABASENAME, null, 33);
		c = context;
		//getWritableDatabase();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL("CREATE TABLE if not exists "+PEOPLE_TABLE+"("
				+ colPeopleId
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ colThreadID
				+ " INTEGER,"
				+ colPhoneNumber
				+ " TEXT,"
				+ colDisplayName
				+ " TEXT,"
				+ colHasSms
				+ " INTEGER,"
				+ colRecentMes
				+ " TEXT,"
				+ colAvatarID
				+ " TEXT,"
				+ colDate
				+ " INTEGER"+
				")");
		
		db.execSQL("CREATE TABLE if not exists "+CONFIG_TABLE+"("
				+ colID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ colKey
				+ " TEXT,"
				+ colValue
				+ " TEXT"+
				")");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP PEOPLE_TABLE IF EXISTS" + PEOPLE_TABLE);
		db.execSQL("DROP CONFIG_TABLE IF EXISTS" + CONFIG_TABLE);
		onCreate(db);
	}
	
	public void addConfig(Config config) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put(colKey, config.key);
		contentValues.put(colValue, config.value);
		
		db.insert(CONFIG_TABLE, null, contentValues);
		db.close();

	}

	// update

	public void updateConfig(Config config) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put(colKey, config.key);
		contentValues.put(colValue, config.value);
		db.update(CONFIG_TABLE, contentValues,colKey +"="+ config.key  , null);

		db.close();
	}

	public void emptyConfig() {
		try {
			SQLiteDatabase db = this.getWritableDatabase();
			db.execSQL("delete from " + CONFIG_TABLE);
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void removeConfig(String key) {
		try {

			String[] args = { key };
			getWritableDatabase().delete(CONFIG_TABLE, colKey +"=?" , args);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Config getConfig(String key){
		Config conf = new Config();
		SQLiteDatabase db = this.getWritableDatabase();
		String[] args = {key};
		Cursor cursor = db.query(CONFIG_TABLE, null, colKey + "=?",args , null, null, null);
		if(cursor.getCount()==0){
			return null;
		}
		cursor.moveToFirst();
		conf.key = key;
		conf.value = cursor.getString(cursor.getColumnIndex(colValue));
		return conf;
	}
	
	
	
	
	public void addPeople(PeopleModel peopleitem) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put(colThreadID, peopleitem.threadID);
		contentValues.put(colPhoneNumber, peopleitem.phoneNumber);
		contentValues.put(colDisplayName, peopleitem.displayName);
		contentValues.put(colHasSms, peopleitem.hasSms);
		contentValues.put(colRecentMes, peopleitem.recentMessage);
		contentValues.put(colAvatarID, peopleitem.avatarID);
		contentValues.put(colDate, peopleitem.date);
		
		db.insert(PEOPLE_TABLE, null, contentValues);
		db.close();

	}

	// update

	public void updatePeople(PeopleModel peopleitem) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues contentValues = new ContentValues();
		contentValues.put(colThreadID, peopleitem.threadID);
		contentValues.put(colPhoneNumber, peopleitem.phoneNumber);
		contentValues.put(colDisplayName, peopleitem.displayName);
		contentValues.put(colHasSms, peopleitem.hasSms);
		contentValues.put(colRecentMes, peopleitem.recentMessage);
		contentValues.put(colAvatarID, peopleitem.avatarID);
		contentValues.put(colDate, peopleitem.date);
		
		String [] args = {String.valueOf(peopleitem.threadID)};
		db.update(PEOPLE_TABLE, contentValues,colThreadID +"=?", args);

		db.close();
	}

	public void emptyPeople() {
		try {
			SQLiteDatabase db = this.getWritableDatabase();
			db.execSQL("delete from " + PEOPLE_TABLE);
			db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void removePeople(int threadID) {
		try {

			String[] args = { String.valueOf(threadID) };
			getWritableDatabase().delete(PEOPLE_TABLE, colThreadID +"=?" , args);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public PeopleModel getPeopleByThreadID(int threadID){
		SQLiteDatabase db = this.getWritableDatabase();
		String args[] = {String.valueOf(threadID)};
		Cursor cursor = db.query(PEOPLE_TABLE, null, colThreadID +"=?", args , null, null, null);
		
		if(!cursor.moveToFirst()){
			return null;
		}
		
		String phoneNumber = cursor.getString(cursor.getColumnIndex(colPhoneNumber));
		String displayName = cursor.getString(cursor.getColumnIndex(colDisplayName));
		String recentMes = cursor.getString(cursor.getColumnIndex(colRecentMes));
		int hasSms = cursor.getInt((cursor.getColumnIndex(colHasSms)));
		long date = cursor.getLong((cursor.getColumnIndex(colDate)));
		int avatarID = cursor.getInt(cursor.getColumnIndex(colAvatarID));
		
		PeopleModel people = new PeopleModel(threadID,phoneNumber,displayName,hasSms,recentMes, avatarID,date);
		return people;
		
		
	}
	public ArrayList<PeopleModel> getPeople() {

		peopleList.clear();

		SQLiteDatabase db = this.getWritableDatabase();
		//Cursor cursor = db.rawQuery("select * from "+ PEOPLE_TABLE, null);
		String orderBy = colDate + " DESC";
		Cursor cursor = db.query(PEOPLE_TABLE, null, null, null, null, null, orderBy);
		if (cursor.getCount() != 0) {
			if (cursor.moveToFirst()) {
				do {
					PeopleModel item = new PeopleModel();

					item.phoneNumber = cursor.getString(cursor
							.getColumnIndex(colPhoneNumber));
					String displayName = cursor.getString(cursor
							.getColumnIndex(colDisplayName));
					if(displayName.equals("?")){
						displayName = item.phoneNumber;
					}
					item.displayName = displayName;
					item.hasSms = cursor.getInt(cursor.getColumnIndex(colHasSms));
					item.date = cursor.getLong(cursor.getColumnIndex(colDate));
					item.recentMessage = cursor.getString(cursor.getColumnIndex(colRecentMes));
					item.avatarID = cursor.getInt(cursor.getColumnIndex(colAvatarID));
					item.threadID = cursor.getInt(cursor.getColumnIndex(colThreadID));
					peopleList.add(item);

				} while (cursor.moveToNext());
			}
		}
		cursor.close();
		db.close();
		return peopleList;
	}

	
}
